﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using DoradoFramework.Core;
using Eflong.Config;
using Eflong.Utility;

namespace Eflong.Tasks
{
    [Description( "KJLAI转储任务" )]
    internal class KJLAITask : TaskTemplate<KJLAITask>
    {
        public override bool Enabled
        {
            get
            {
                return true;
            }
            set
            {
                _enabled = value;
            }
        }

        /// <summary>
        /// 系统配置
        /// </summary>
        public KWBridgeConfig KWConfig
        {
            get { return KWBridgeConfig.Instance; }
        }

        /// <summary>
        /// 创建内存定时回收任务
        /// </summary>
        protected override bool CreateTask( )
        {
            if ( KWConfig.KJSysParas.KJType.ToUpper( ) == "KJLAI" )
            {
                if ( TaskHandle == null )
                {
                    TaskHandle = GlobalTimer<ITask>.Default.Add( TimeSpan.FromSeconds( 5 ) , new TaskFuncAdapter( ( ) =>
                    {
                        try
                        {
                            string kjPath = KWConfig.KJSysParas.KJPath;
                            if ( !Directory.Exists( kjPath ) ) return;

                            FileInfo[ ] files = new DirectoryInfo( kjPath ).GetFiles( );
                            IEnumerable<FileInfo> devFile = files.Where( n => n.Name.StartsWith( "dev_" ) );
                            if ( devFile == null || devFile.Count( ) == 0 ) return;
                            IEnumerable<FileInfo> rtdata = files.Where( n => n.Name.StartsWith( "rtdata_" ) );
                            if ( rtdata == null || rtdata.Count( ) == 0 ) return;

                            string lastDevFile = devFile.Last( ).FullName;
                            string lastRtdataFile = rtdata.Last( ).FullName;
                            string newDevPath = Path.Combine( kjPath , KWConfig.KJSysParas.DevFile );
                            string newRtdataPath = Path.Combine( kjPath , KWConfig.KJSysParas.RtDataFile );

                            using ( StreamWriter swDev = new StreamWriter( newDevPath , false , Encoding.Default ) )
                            {
                                swDev.WriteLine( File.ReadAllText( lastDevFile , Encoding.UTF8 ) );
                            }

                            using ( StreamWriter swRtdata = new StreamWriter( newRtdataPath , false , Encoding.Default ) )
                            {
                                swRtdata.WriteLine( File.ReadAllText( lastRtdataFile , Encoding.ASCII ) );
                            }
                        }
                        catch ( IOException )
                        {
                        }
                        catch ( Exception ex )
                        {
                            LogWrapper.Logger.Error( ex );
                        }
                    } ) , false );//不允许任务重入
                }
                else
                {
                    TaskHandle.Start( );
                }
                return true;
            }
            else
            {
                DisposeTask( );
                return false;
            }
        }

        /// <summary>
        /// 销毁内存定时回收任务
        /// </summary>
        /// <returns></returns>
        protected override bool DisposeTask( )
        {
            if ( TaskHandle != null )
            {
                TaskHandle.Dispose( );
                TaskHandle = null;
                return true;
            }
            return false;
        }
    }
}